package com.sphd.beautysalon.fragments;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.sphd.beautysalon.adapters.DhAdapter;
import com.sphd.beautysalon.base.RestaurantUrl;
import com.sphd.beautysalon.info.LoginInfo;
import com.sphd.beautysalon.interfaces.IntegralCallBack;
import com.sphd.beautysalon.presenter.IntegralRecordPresenterImpl;
import com.sphd.beautysalon.utils.Constant;
import com.sphd.beautysalon.view.IntegralRecordView;
import com.sphd.beautysalon.R;
import com.sphd.beautysalon.base.LazyLoadFragment;
import com.sphd.beautysalon.info.DuihuanInfo;
import com.sphd.beautysalon.info.RestaurantInfo;
import com.sphd.beautysalon.info.VipInfo;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Hu on 2016/11/14.
 */

public class DhjfHistoryFragment extends LazyLoadFragment implements IntegralRecordView, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.my_recy)
    RecyclerView myRecy;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    VipInfo vipInfo;
    RestaurantInfo restaurantInfo;
    private LoginInfo loginInfo;
    Bundle bundle;
    IntegralRecordPresenterImpl integralRecordPresenter;
    private static final String TAG = "DhjfHistoryFragment";
    private DhAdapter dhAdapter;
    IntegralCallBack integralCallBack;
    @Override
    public int getLayout() {
        return R.layout.swf_and_recy;
    }

    @Override
    public void initViews(View rootView) {
        ButterKnife.bind(this, rootView);
        init();
        initEvent();
    }

    private void initEvent() {
        mySwf.setOnRefreshListener(this);
    }

    private void init() {
        integralRecordPresenter = new IntegralRecordPresenterImpl(this);
        initAdapter();
        vipInfo = (VipInfo) bundle.getSerializable(Constant.VIPINFO);
        loginInfo = DataSupport.findLast(LoginInfo.class);
        restaurantInfo = (RestaurantInfo) bundle.getSerializable(Constant.RESTAURANTSINFO);
        Log.e(TAG, vipInfo.getMem_phone());
    }

    private void initAdapter() {
        setRefresh(mySwf);
        setRecy(myRecy);
        dhAdapter = new DhAdapter(R.layout.layout_jifen_item, new ArrayList<>());
        myRecy.setAdapter(dhAdapter);
    }

    @Override
    public void loadData() {
        addData();
    }

    private void addData() {
        integralRecordPresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.GETJIFENHISTORY + "cgid=" +
                vipInfo.getMem_cgid() + "&mem_card=" +
                vipInfo.getMem_card() + "&mem_phone=" + vipInfo.getMem_phone() + "&status=0");
//        integralRecordPresenter.addData("http://cake.zhongxs.com/Restaurant/cg/manager_list_integral_gain.action?cgid=284&mem_card=null&mem_phone=13212345678&status=0");

    }


    public void setVal(Bundle bundle,IntegralCallBack integralCallBack) {
        this.bundle = bundle;
        this.integralCallBack=integralCallBack;
    }

    @Override
    public void recRes(List<DuihuanInfo> duihuanInfos, int integral) {
        dhAdapter.setNewData(duihuanInfos);
        integralCallBack.returnIntegral(integral);

    }

    @Override
    public void onRefresh() {
        addData();
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
        mySwf.setRefreshing(false);
    }
}
